AIOLIBS := $(LIBAIO_DIR)/libaio.a
-BLK-OBJS := block-aio.o
-BLK-OBJS += block-sync.o
-BLK-OBJS += block-vmdk.o
-BLK-OBJS += block-ram.o
-BLK-OBJS += block-qcow.o
-BLK-OBJS += aes.o
-BLK-OBJS += tapaio.o
+BLK-OBJS-y := block-aio.o
+BLK-OBJS-y += block-sync.o
+BLK-OBJS-y += block-vmdk.o
+BLK-OBJS-y += block-ram.o
+BLK-OBJS-y += block-qcow.o
+BLK-OBJS-y += aes.o
+BLK-OBJS-y += tapaio.o
+BLK-OBJS-$(CONFIG_Linux) += blk_linux.c
all: $(IBIN) qcow-util
blktapctrl: blktapctrl.c
$(CC) $(CFLAGS) -o blktapctrl $(LIBS) blktapctrl.c
-tapdisk: $(BLK-OBJS) tapdisk.c
- $(CC) $(CFLAGS) -o tapdisk $(BLK-OBJS) tapdisk.c \
+tapdisk: $(BLK-OBJS-y) tapdisk.c
+ $(CC) $(CFLAGS) -o tapdisk $(BLK-OBJS-y) tapdisk.c \
$(AIOLIBS) $(LIBS)
.PHONY: qcow-util
qcow-util: img2qcow qcow2raw qcow-create
-img2qcow qcow2raw qcow-create: %: $(BLK-OBJS)
- $(CC) $(CFLAGS) -o $* $(BLK-OBJS) $*.c $(AIOLIBS) $(LIBS)
+img2qcow qcow2raw qcow-create: %: $(BLK-OBJS-y)
+ $(CC) $(CFLAGS) -o $* $(BLK-OBJS-y) $*.c $(AIOLIBS) $(LIBS)
install: all
$(INSTALL_PROG) $(IBIN) $(QCOW_UTIL) $(VHD_UTIL) $(DESTDIR)$(INST_DIR)
--- /dev/null
+
+int blk_getimagesize(int fd, uint64_t *size);
+int blk_getsectorsize(int fd, uint64_t *sector_size);
--- /dev/null
+#include <inttypes.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#include "tapdisk.h"
+#include "blk.h"
+
+int blk_getimagesize(int fd, uint64_t *size)
+{
+ int rc;
+
+ *size = 0;
+ rc = ioctl(fd, BLKGETSIZE, size);
+ if (rc) {
+ DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+int blk_getsectorsize(int fd, uint64_t *sector_size)
+{
+#if defined(BLKSSZGET)
+ int rc;
+
+ *sector_size = DEFAULT_SECTOR_SIZE;
+ rc = ioctl(fd, BLKSSZGET, sector_size);
+ if (rc) {
+ DPRINTF("ERR: BLKSSZGET failed. Falling back to use default sector size");
+ *sector_size = DEFAULT_SECTOR_SIZE;
+ }
+
+ if (*sector_size != DEFAULT_SECTOR_SIZE)
+ DPRINTF("Note: sector size is %"PRIu64" (not %u)\n",
+ *sector_size, DEFAULT_SECTOR_SIZE);
+#else
+ *sector_size = DEFAULT_SECTOR_SIZE;
+#endif
+
+ return 0;
+}
+
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include "tapdisk.h"
#include "tapaio.h"
+#include "blk.h"
#define MAX_AIO_REQS (MAX_REQUESTS * MAX_SEGMENTS_PER_REQ)
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
+
struct pending_aio {
td_callback_t cb;
int id;
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
- s->size = 0;
- if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
- DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
+ if (blk_getimagesize(fd, &s->size) != 0)
return -EINVAL;
- }
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)s->size);
/*Get the sector size*/
-#if defined(BLKSSZGET)
- {
- int arg;
+ if (blk_getsectorsize(fd, &s->sector_size) != 0)
s->sector_size = DEFAULT_SECTOR_SIZE;
- ioctl(fd, BLKSSZGET, &s->sector_size);
-
- if (s->sector_size != DEFAULT_SECTOR_SIZE)
- DPRINTF("Note: sector size is %ld (not %d)\n",
- s->sector_size, DEFAULT_SECTOR_SIZE);
- }
-#else
- s->sector_size = DEFAULT_SECTOR_SIZE;
-#endif
} else {
/*Local file? try fstat instead*/
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include <string.h>
#include <zlib.h>
#include <inttypes.h>
#include "aes.h"
#include "tapdisk.h"
#include "tapaio.h"
+#include "blk.h"
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
#if 1
#define ASSERT(_p) \
fd = open(filename, O_RDONLY);
if (fd < 0)
return -1;
- if (ioctl(fd,BLKGETSIZE,size)!=0) {
- printf("Unable to get Block device size\n");
+ if (blk_getimagesize(fd, size) != 0) {
close(fd);
return -1;
}
if (!final_cluster)
s->fd_end = s->l1_table_offset + l1_table_size;
else {
- s->fd_end = lseek64(fd, 0, SEEK_END);
- if (s->fd_end == (off64_t)-1)
+ s->fd_end = lseek(fd, 0, SEEK_END);
+ if (s->fd_end == (off_t)-1)
goto fail;
}
DPRINTF("Qcow_create: size %llu\n",(long long unsigned)total_size);
fd = open(filename,
- O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
+ O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
0644);
if (fd < 0)
return -1;
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <inttypes.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include <string.h>
#include "tapdisk.h"
+#include "blk.h"
#define MAX_DISK_SIZE 1024000 /*500MB disk limit*/
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
+
char *img;
long int disksector_size;
long int disksize;
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
- s->size = 0;
- if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
- DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
+ if (blk_getimagesize(fd, &s->size) != 0)
return -EINVAL;
- }
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)s->size);
/*Get the sector size*/
-#if defined(BLKSSZGET)
- {
- int arg;
+ if (blk_getsectorsize(fd, &s->sector_size) != 0)
s->sector_size = DEFAULT_SECTOR_SIZE;
- ioctl(fd, BLKSSZGET, &s->sector_size);
-
- if (s->sector_size != DEFAULT_SECTOR_SIZE)
- DPRINTF("Note: sector size is %ld (not %d)\n",
- s->sector_size, DEFAULT_SECTOR_SIZE);
- }
-#else
- s->sector_size = DEFAULT_SECTOR_SIZE;
-#endif
} else {
/*Local file? try fstat instead*/
disksector_size = s->sector_size;
disksize = s->size;
diskinfo = s->info;
- DPRINTF("Image sector_size: \n\t[%lu]\n",
+ DPRINTF("Image sector_size: \n\t[%"PRIu64"]\n",
s->sector_size);
return 0;
"sector_shift [%llu]\n",
(long long unsigned)(s->size << SECTOR_SHIFT),
(long long unsigned)s->size);
- DPRINTF("Image sector_size: \n\t[%lu]\n",
+ DPRINTF("Image sector_size: \n\t[%"PRIu64"]\n",
s->sector_size);
prv->fd = -1;
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include "tapdisk.h"
+#include "blk.h"
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
struct tdsync_state {
int fd;
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
- s->size = 0;
- if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
- DPRINTF("ERR: BLKGETSIZE failed, couldn't stat image");
+ if (blk_getimagesize(fd, &s->size) != 0)
return -EINVAL;
- }
DPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)s->size);
/*Get the sector size*/
-#if defined(BLKSSZGET)
- {
- int arg;
+ if (blk_getsectorsize(fd, &s->sector_size) != 0)
s->sector_size = DEFAULT_SECTOR_SIZE;
- ioctl(fd, BLKSSZGET, &s->sector_size);
-
- if (s->sector_size != DEFAULT_SECTOR_SIZE)
- DPRINTF("Note: sector size is %ld (not %d)\n",
- s->sector_size, DEFAULT_SECTOR_SIZE);
- }
-#else
- s->sector_size = DEFAULT_SECTOR_SIZE;
-#endif
} else {
/*Local file? try fstat instead*/
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include <string.h>
#include "tapdisk.h"
#include "bswap.h"
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
+
#define safer_free(_x) \
do { \
if (NULL != _x) { \
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include <string.h>
#include "tapdisk.h"
+#include "blk.h"
#if 1
#define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
#define DFPRINTF(_f, _a...) ((void)0)
#endif
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
+
+
#define TAPDISK 1
#define BLOCK_PROCESSSZ 4096
if (S_ISBLK(stat.st_mode)) {
/*Accessing block device directly*/
- s->size = 0;
- if (ioctl(fd,BLKGETSIZE,&s->size)!=0) {
- DFPRINTF("ERR: BLKGETSIZE failed, "
- "couldn't stat image");
+ if (blk_getimagesize(fd, &s->size) != 0)
return -EINVAL;
- }
DFPRINTF("Image size: \n\tpre sector_shift [%llu]\n\tpost "
"sector_shift [%llu]\n",
(long long unsigned)s->size);
/*Get the sector size*/
-#if defined(BLKSSZGET)
- {
- int arg;
+ if (blk_getsectorsize(fd, &s->sector_size) != 0)
s->sector_size = DEFAULT_SECTOR_SIZE;
- ioctl(fd, BLKSSZGET, &s->sector_size);
-
- if (s->sector_size != DEFAULT_SECTOR_SIZE)
- DFPRINTF("Note: sector size is %ld (not %d)\n",
- s->sector_size, DEFAULT_SECTOR_SIZE);
- }
-#else
- s->sector_size = DEFAULT_SECTOR_SIZE;
-#endif
} else {
/*Local file? try fstat instead*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <inttypes.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include <string.h>
#include "tapdisk.h"
+#include "blk.h"
#if 1
#define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
#define DFPRINTF(_f, _a...) ((void)0)
#endif
+
+/* *BSD has no O_LARGEFILE */
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
+
#define TAPDISK 1
#define BLOCK_PROCESSSZ 4096
int main(int argc, char *argv[])
{
int ret = -1, fd, len,input;
- long int size;
+ uint64_t size;
fd_set readfds;
struct timeval timeout;
uint64_t i;
}
if (S_ISBLK(finfo.st_mode)) {
- if(ioctl(fd,BLKGETSIZE,&size)!=0) {
- DFPRINTF("ERROR: BLKGETSIZE failed, "
- "couldn't stat image [%s]\n",
- argv[1]);
+ if (blk_getimagesize(fd, &size) != 0) {
close(fd);
- exit(-1);
+ return -1;
}
+
if (size < ddqcow.td_state->size<<9) {
DFPRINTF("ERROR: Not enough space on device "
- "%s (%lu bytes available, %llu bytes required\n",
+ "%s (%"PRIu64" bytes available, "
+ "%llu bytes required\n",
argv[1], size,
(long long unsigned)ddqcow.td_state->size<<9);
close(fd);
void *image;
void *ring_info;
void *fd_entry;
- unsigned long sector_size;
- unsigned long long size;
+ uint64_t sector_size;
+ uint64_t size;
unsigned int info;
};